package com.af.security.auth.user.data;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : zhenyun.su
 * @comment: 获取认证数据，可公共
 * @since : 2020/03/25
 */

public class AfBaseAuth {
    public AfBaseAuth() {

    }

    public Authentication getAuthentication(){
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public Boolean isAuthenticated() {
        return (this.getAuthentication() != null) && (this.getAuthentication().isAuthenticated());
    }

    public Object getName(){
        if (isAuthenticated()) {
            return this.getAuthentication().getName();
        }
        return null;
    }

    public Object getPrincipal(){
        if (isAuthenticated()) {
            return this.getAuthentication().getPrincipal();
        }
        return null;
    }

    public Object getDetails(){
        if (isAuthenticated()) {
            return this.getAuthentication().getDetails();
        }
        return null;
    }
    public Object getCredentials(){
        if (isAuthenticated()) {
            return this.getAuthentication().getCredentials();
        }
        return null;
    }

    public List<String> getAuthorities() {
        List<String> items = new ArrayList<>();
        if (isAuthenticated()) {
            getAuthentication().getAuthorities().forEach(authority -> {
                items.add(authority.getAuthority());
            });
        }
        return items;
    }
}
